package com.ld.task;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程锁的使用
 */
public class UseLock {

    Lock lock = new ReentrantLock(); // 可重入锁

    public void useLock() {
        lock.lock();  //阻塞方法，一直在等待获取锁
        try {
            System.out.println("Thread:" + Thread.currentThread().getName() + "运行。。。");
            Thread.sleep(3000);
            useLock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void useTryLock() {
        if (lock.tryLock()) { //尝试获取锁，获取不到返回false
            try {
                System.out.println("Thread:" + Thread.currentThread().getName() + "运行。。。");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        } else {
            System.out.println("Thread:" + Thread.currentThread().getName() +"线程锁获取失败。");
        }

    }

    public void useTryLockTimeout() throws InterruptedException {
        if (lock.tryLock(1, TimeUnit.SECONDS)) {  //尝试获取锁，等待一段时间后还是获取不到再返回false
            try {
                System.out.println("Thread:" + Thread.currentThread().getName() + "运行。。。");
                Thread.sleep(7000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        } else {
            System.out.println("Thread:" + Thread.currentThread().getName() +"线程锁获取失败。");
        }

    }

    public static void main(String[] args) {
        UseLock useLock = new UseLock();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setName("useLock1");
                useLock.useLock();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setName("useLock2");
                useLock.useLock();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setName("useLock3");
                useLock.useLock();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setName("useTryLock1");
                useLock.useTryLock();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setName("useTryLock2");
                useLock.useTryLock();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.currentThread().setName("useTryLockTimeout1");
                    useLock.useTryLockTimeout();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.currentThread().setName("useTryLockTimeout2");
                    useLock.useTryLockTimeout();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
